﻿using System;
using System.Collections.Generic;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Tp.Dto;
using Tp.Dto.EventArgs;
using Tp.Interfaces.Commands;
using Tp.Interfaces.Services;
using Tp.Interfaces.ViewModels;

namespace Tp.GeschaeftsfaelleViewModel
{
	public class GeschaeftsfaelleUserControlViewModel : ViewModelBase, IGeschaeftsfaelleUserControlViewModel
	{
		#region Fields
		private IEmployeeService _service;
		private List<EmployeeDto> _employeeList;
		private EmployeeDto _selectedEmployee;
		#endregion

		#region Properties
        public IGeschaeftsfaelleViewModelCommands Commands { get; set; }

		public List<EmployeeDto> EmployeeList
		{
			get { return this._employeeList; }
			set
			{
				if (this._employeeList != null && this._employeeList == value)
				{
					return;
				}

				this._employeeList = value;
				RaisePropertyChanged(() => EmployeeList);
			}
		}

		public EmployeeDto SelectedEmployee
		{
			get { return this._selectedEmployee; }
			set
			{
				if (this._selectedEmployee == value)
				{
					return;
				}

				this._selectedEmployee = value;
				RaisePropertyChanged(() => SelectedEmployee);
			}
		}

		#endregion

		#region C-Tor
		public GeschaeftsfaelleUserControlViewModel(IGeschaeftsfaelleViewModelCommands comands, IEmployeeService service)
		{													
		    this.Commands = comands;
			this._service = service;
			this.RegisterMessengers();	
		}
		#endregion

		#region Messengers
		private void RegisterMessengers()
		{
			Messenger.Default.Register<GeschaeftsfallOperationEventArgs>(this, ExecuteMessage);
		}
		#endregion

		#region Private methods
		private void ExecuteMessage(GeschaeftsfallOperationEventArgs ea)
		{
			switch (ea.OperationName.ToLowerInvariant())
			{
				case "loademployees":
					this.LoadEmployees();
					break;
				default:
					throw new NotImplementedException(string.Format("Message of type '{0}' is not implemented"));
			}
		}

		private void LoadEmployees()
		{
			EmployeeList = this._service.LoadEmployees();
		}
		#endregion
	}
}